package com.xdm.impl;

import java.util.HashMap;
import java.util.List;

import com.xdm.XManager;
import com.xdm.ds.XmDataSourceFactory;
import com.xdm.ds.XmItem;
import com.xdm.resource.XmResourceFactory;
import com.xdm.ui.XmItemPanel;
import com.xdm.ui.XmItemPanelView;
import com.xdm.ui.XmListAdapter;
import com.xdm.ui.XmUiFactory;
import com.xdm.ui.adapter.android.R;

/**
 * XManagerImpl's generic implementation
 * 
 * @author Alexander Lapygin
 */
public class XManagerImpl implements XManager {
	public XmUiFactory xmUiFactory;
	public XmDataSourceFactory xmDsFactory;
	public XmResourceFactory xmResourceFactory;
	
	private HashMap<String, XmItemPanel> panelMap = new HashMap<String, XmItemPanel>();

	public XManagerImpl(XmUiFactory xmUiFactory, XmDataSourceFactory xmDsFactory, XmResourceFactory xmResourceFactory) {
		this.xmUiFactory = xmUiFactory;
		this.xmDsFactory = xmDsFactory;
		this.xmResourceFactory = xmResourceFactory;
		initPanel(R.id.list1 + "");
		initPanel(R.id.list2 + "");
	}

	public void onItemClick(XmItemPanelView parent, int position) {
		XmItemPanel itemPanel = getItemPanel(parent.getId());
		XmItem currentDirectory = itemPanel.getDirectory();
		if (!isRoot(currentDirectory)) {
			if (position == 0) { // ".."
				fillPanel(itemPanel, currentDirectory.getParentItem());
				return;
			}
			position--;
		}
		String path = itemPanel.getItems().get(position).getPath();
		XmItem item = xmDsFactory.newItem(path);
		if (item.isDirectory()) {
			fillPanel(itemPanel, item);
		} else {
			xmUiFactory.showAlert("No directory", "It's plain file:" + path, "Ok", false);
		}
	}

	private void initPanel(String srtViewId) {
		XmItemPanel itemPanel = xmUiFactory.newPanel();
		panelMap.put(srtViewId, itemPanel);
		XmItemPanelView itemPanelView = xmResourceFactory.getView(srtViewId);
		itemPanelView.setOnItemClickListener(xmUiFactory.newOnItemClickHandler(this));
		itemPanel.setView(itemPanelView);
		fillPanel(itemPanel, xmDsFactory.newItem("/"));
	}

	private void fillPanel(XmItemPanel itemPanel, XmItem directory) {
		List<XmItem> items = directory.getChildren();
		itemPanel.setDirectory(directory);
		itemPanel.setItems(items);

		XmListAdapter listAdapter = xmUiFactory.newListAdapter();
		if (itemPanel.getDirectory().getParent() != null) {
			listAdapter.addItem(xmUiFactory.newDecoratedText("..", xmResourceFactory.getImage(R.drawable.up + "")));
		}
		for (XmItem item : items) {
			int id = item.isDirectory() ? R.drawable.folder_blue : R.drawable.file_doc;
			listAdapter.addItem(xmUiFactory.newDecoratedText(item.getName(), xmResourceFactory.getImage(id + "")));
		}
		itemPanel.getView().setAdapter(listAdapter);
	}

	private XmItemPanel getItemPanel(String id) {
		return panelMap.get(id);
	}

	public XmItemPanelView getItemPanelView(String id) {
		return getItemPanel(id).getView();
	}
	
	private boolean isRoot(XmItem currentDirectory) {
		return currentDirectory.getParent() == null;
	}
}
